<template>
    <div class="content">
        <div class="content1">
            <van-search @click="goto" shape="round" background="#17171a" :placeholder="$t('搜索')" />
            <van-tabs v-model:active="active" color="#17171a" background="#17171a" title-inactive-color="#fff"
                title-active-color="#f2ce3d">
                <van-tab title="Ethereum"></van-tab>
                <van-tab title="BNB Smart Chain"></van-tab>
                <van-tab title="Arbitrum One"></van-tab>
                <van-tab title="zkSync"></van-tab>
            </van-tabs>
            <van-tabs v-model:active="active1" color="#17171a" background="#17171a" title-inactive-color="#fff"
                title-active-color="#f2ce3d">
                <van-tab :title="$t('自選')"></van-tab>
                <van-tab :title="$t('現貨')"></van-tab>
                <van-tab :title="$t('合約')"></van-tab>
            </van-tabs>
        </div>
        <div class="cont">
            <div class="text">USDT</div>
            <div class="title">
                <div class="left">
                    {{ $t('市场') }}/{{ $t('成交量') }}
                </div>
                <div class="right">
                    <div class="le">{{ $t('最新价') }}</div>
                    <div class="ri">24H{{ $t('涨跌幅') }}</div>
                </div>
            </div>
            <ul class="ul">
                <li class="item" v-for="item in coinList">
                    <div class="left">
                        <div class="top">
                            <span style="color: #fff;">{{ item.data?.s.replace(/([A-Z]+)(USDT)/, '$1/$2') }}</span>
                        </div>
                        <div class="btm">
                            24H{{ $t('成交量') }} {{ (item.data?.n).toFixed(2) }}
                        </div>
                    </div>
                    <div class="right">
                        <div class="le">
                            <div class="top"
                                :style="{ color: ((item.data?.c - item.data?.x) / item.data?.x) >= 0 ? '#2ebd85' : '#f5465d' }">
                                {{ (+item.data?.c).toFixed(2) }}</div>
                            <div class="btm">${{ (+item.data?.c).toFixed(2) }}</div>
                        </div>
                        <div @click="gotoview" class="ri"
                            :style="{ backgroundColor: ((item.data?.c - item.data?.x) / item.data?.x) >= 0 ? '#2ebd85' : '#f5465d' }">
                            {{ (((item.data?.c - item.data?.x) / item.data?.x) * 100).toFixed(2) }}%</div>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</template>

<script setup>
import { ref, onBeforeUnmount } from 'vue';
import { useRouter } from 'vue-router';
const router = useRouter();

const active = ref(0);
const active1 = ref(1);
const coinList = ref([])

function goto() {
    router.push('/search');
}

function gotoview() {
    router.push('/view');
}


//WebSocket ----------------------------------------------------------------------------------------------------------
// 创建 WebSocket 连接
const socket = new WebSocket("wss://stream.binance.com:9443/stream?streams=btcusdt@ticker/ethusdt@ticker/bnbusdt@ticker/filusdt@ticker/ltcusdt@ticker/uniusdt@ticker/mkrusdt@ticker/arbusdt@ticker");
// 添加事件处理程序来处理不同的 WebSocket 事件
socket.onopen = (event) => {
    console.log("WebSocket 连接已打开");
    // 在连接打开后发送消息到服务器
    const message =
    {
        "method": "SUBSCRIBE",
        "params":
            [
                "btcusdt@ticker",
                "ethusdt@ticker",
                "bnbusdt@ticker",
                "filusdt@ticker",
                "ltcusdt@ticker",
                "uniusdt@ticker",
                "mkrusdt@ticker",
                "arbusdt@ticker",
            ],
        "id": 1
    }
        ;
    socket.send(JSON.stringify(message));
};
socket.onmessage = (event) => {
    const data = JSON.parse(event.data);
    // if (data.stream === "btcusdt@ticker") {
    // }
    // console.log("收到消息：", data);
    if (data.result === null) return
    //如果数组中没有这个币种，就添加进去
    let isExist;
    if (coinList.value.length !== 0) {
        // console.log(coinList.value);
        isExist = coinList.value.findIndex(item => {
            return item.stream === data.stream
        })
    }
    // console.log(isExist);

    if (isExist === -1 || coinList.value.length === 0) {
        coinList.value.push(data);
    } else {
        coinList.value[isExist] = data;
    }


};
socket.onclose = (event) => {
    console.log(event);
    if (event.wasClean) {
        console.log(`WebSocket 连接已关闭，状态码：${event.code}，原因：${event.reason}`);
    } else {
        console.error("WebSocket 连接断开");
    }
};
socket.onerror = (error) => {
    console.error("WebSocket 错误：", error);
};

//WebSocket ----------------------------------------------------------------------------------------------------------

onBeforeUnmount(() => {
    console.log('页面卸载了');
    socket.close();
});

</script>

<style lang="scss" scoped>
.content {
    background-color: #17171a;
    padding-top: 44px;
    min-height: calc(100vh - 44px);
}

.content1 {
    padding: 10px 15px 0 15px;
    // padding-top: 44px;
}

.van-search {
    background-color: #17171a;
}

::v-deep .van-search__content {
    background: #232830;
}

::v-deep .van-tab {
    flex: none;
    padding: 0 10px;
}

.cont {
    padding: 15px;
    background-color: #2e323b;
    color: #fff;

    border-radius: 20px 20px 0px 0px;

    .text {
        font-size: 17px;
        color: #f2ce3d;
        margin-left: 12px;
        margin-bottom: 10px;
    }

    .title {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
        color: #a8aeae;
        margin-bottom: 8px;

        .right {
            display: flex;
            justify-content: space-between;

            .le {
                margin-right: 35px;
            }
        }
    }

    .ul {
        //剩余高度，全部撑满

        .item {
            display: flex;
            justify-content: space-between;
            font-size: 14px;
            color: #fff;
            padding: 12px 0;

            .left {
                color: #fff;

                .top {
                    font-size: 15px;
                    color: #fff;
                }

                .btm {
                    font-size: 12px;
                    color: #d5d5d5;
                }
            }

            .right {
                display: flex;
                justify-content: space-between;

                .le {
                    margin-right: 22px;

                    .top {
                        font-size: 15px;
                    }

                    .btm {
                        color: #d5d5d5;
                        font-size: 13px;
                    }
                }

                .ri {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 70px;
                    height: 100%;
                    border-radius: 5px;
                }
            }
        }
    }
}
</style>